//
//  LanguageSettingVC.swift
//  ImGuider X
//
//  Created by 陆涛 on 2018/12/7.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit

class LanguageSettingVC: BaseVC {
    
    @IBOutlet weak var tableView: UITableView!
    
    @IBOutlet weak var commitBtn: UIButton!
    
    private var currentLan:AppLanguage = AppManager.share.language
    
    var dataSource:[[String:Any]]?
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        setupData()
    }
    
    private func setupUI() {
        
        
        let colors = [UIColor.hexStringToColor(hexString: "#A8A8A8"),UIColor.hexStringToColor(hexString: "#505050")]
        let layer = CAGradientLayer.gradientLayer(colors: colors, position:.topToBottom, frame: CGRect(x: 0, y: 0, width: kScreenWidth - 32, height: 50))
        layer.cornerRadius = 10
        commitBtn.layer.insertSublayer(layer, at: 0)
        
        commitBtn.setTitle(LocalizedString("confirm"), for: .normal)
        
        tableView.rowHeight = 60
        tableView.estimatedRowHeight = 60
        
        tableView.register(UINib(nibName: "LanguageCell", bundle: nil), forCellReuseIdentifier: "LanguageCell")
        
        self.title = LocalizedString("language_setting")
    }
    
    private func setupData() {
        
        dataSource = [["name":"简体中文","lan":AppLanguage.chinese],
                      ["name":"English","lan":AppLanguage.english]]
    }
    
    @IBAction func commitAction(_ sender: Any) {
        
        if currentLan == AppManager.share.language {
            
            self.navigationController?.popViewController(animated: true)
            return
        }
        
        AppManager.share.language = currentLan
        AppManager.share.HomePage()
    }
}

extension LanguageSettingVC: UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataSource?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "LanguageCell") as! LanguageCell
        
        let config = dataSource?[indexPath.row];
        
        cell.languageLabel.text = config?["name"] as? String
        
        if let lan = config?["lan"] as? AppLanguage, lan == currentLan {
            
            cell.signImageView.image = UIImage(named: "button_tick_sel")
        } else {
            
            cell.signImageView.image = UIImage(named: "button_tick_nor")
        }
        
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let config = dataSource?[indexPath.row];
        
        tableView.deselectRow(at: indexPath, animated: true)
        
        guard let lan = config?["lan"] as? AppLanguage,lan != currentLan else { return }
        
        currentLan = lan
        
        tableView.reloadData()
    }
}

